//
//  ActivityViewController.m
//  豆瓣
//
//  Created by aaaaaa on 16/6/23.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ActivityViewController.h"
#import "ActivityTableViewCell.h"
#import "ActivityModel.h"
#import "OwnerModel.h"
#import "IntroducedViewController.h"

@interface ActivityViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray<ActivityModel *> *activityModelArray;

@end

@implementation ActivityViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self config];
    [self handleData];
    [self createTableView];
}

- (void)config{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:86 / 255.0 green:137 / 255.0 blue:157 / 255.0 alpha:1.0];
    self.navigationItem.title = @"活动";
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)handleData{
    
    self.activityModelArray = [NSMutableArray array];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"activitylist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSError *error = nil;
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    
    NSArray *dataArray = [dictionary objectForKey:@"events"];
    
    for (NSDictionary *dic in dataArray) {
        ActivityModel *activiModel = [[ActivityModel alloc] init];
        OwnerModel *owenerModel = [[OwnerModel alloc] init];
        
        for (NSString *key in dic) {
            if ([key isEqualToString:@"owner"]) {
                NSDictionary *ownerDic  = [dic objectForKey:key];
                [owenerModel setValuesForKeysWithDictionary:ownerDic];
                break;
            }
        }
        
        [activiModel setValuesForKeysWithDictionary:dic];
        activiModel.owner = owenerModel;
        [self.activityModelArray addObject:activiModel];
    }
}

- (void)createTableView{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 64 - 50) style:UITableViewStylePlain];
    
    tableView.separatorColor = [UIColor whiteColor];

    
    tableView.delegate = self;
    tableView.dataSource =self;
    
    [tableView registerClass:[ActivityTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:tableView];
    [tableView release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.activityModelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    ActivityModel *model = self.activityModelArray[indexPath.row];
    cell.activityModel = model;
    
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ActivityModel *model = self.activityModelArray[indexPath.row];
    
    IntroducedViewController * introducedVc = [[IntroducedViewController alloc] init];
    introducedVc.activityModel = model;
    [self.navigationController pushViewController:introducedVc animated:YES];
    [introducedVc release];
    
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
